﻿using AlibabaCloud.OpenApiClient.Models;
using AlibabaCloud.SDK.Alidns20150109;
using AlibabaCloud.SDK.Alidns20150109.Models;
using AlibabaCloud.TeaUtil.Models;
using PluginForDNSRecord;
using Tea;
namespace DNSRecordAliyunCommon;

public class AliyunDnsRecordEditor : IDNSRecordEditor
{
    /// <summary>
    /// 插件的唯一名称
    /// </summary>
    public string Identity => "阿里云DNS记录";
    /// <summary>
    /// 插件描述
    /// </summary>
    public string Description
    {
        get
        {
            return $"在阿里云注册的域名,可以通过该插件编辑域名解析\r\n ";
        }
    }
    /// <summary>
    /// 配置项
    /// </summary>
    public List<ConfigItem> ConfigKey
    {
        get
        {
            return new List<ConfigItem>()
            {
                new ConfigItem(){ IsSecret=true, Key="AccessKeyID", DisplayName="阿里云AccessKeyId" },
                new ConfigItem(){ IsSecret=true, Key="AccessKeySecret", DisplayName="阿里云AccessKeySecret" }
            };
        }
    }
    /// <summary>
    /// 配置值--在执行EditDnsRecord 前，主程序会将配置的值，填充到Config。
    /// 无论是否加密都会返回源字符串。在EditDnsRecord中可以直接使用，不需要解密
    /// </summary>
    public Dictionary<string, string> Config { get; set; }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="domainName">记录名称 domain.com</param>
    /// <param name="recordName">解析记录 _acme-challenge</param>
    /// <param name="Type">解析类型  TXT</param>
    /// <param name="recordValue">记录值</param>
    /// <returns></returns>
    public async Task<EditorResult> EditDnsRecord(string domainName, string recordName, string Type, string recordValue)
    {
        Config config = new Config
        {
            AccessKeyId = Config["AccessKeyID"],
            AccessKeySecret = Config["AccessKeySecret"],
        };
        Client client = new Client(config);
        AddDomainRecordRequest addDomainRecordRequest = new AddDomainRecordRequest
        {
            DomainName = domainName,
            RR = recordName,
            Type = Type.ToUpper(),
            Value = recordValue,
        };

        DeleteSubDomainRecordsRequest deleteSubDomainRecordsRequest = new DeleteSubDomainRecordsRequest
        {
            DomainName = domainName,
            RR = recordName,
        };
        RuntimeOptions runtime = new();
        string msgTemp = $"{domainName}解析记录{recordName},记录类型{Type},记录值{recordValue}";
        try
        {
            //需要先删除再增加
            var delRes = await client.DeleteSubDomainRecordsWithOptionsAsync(deleteSubDomainRecordsRequest, runtime);
            if (delRes.StatusCode != 200)
            {
                return new EditorResult() { Result = false, Message = $"{msgTemp}修改失败",Extention="记录值删除失败" };
            }
            var res = await client.AddDomainRecordWithOptionsAsync(addDomainRecordRequest, runtime);
            if (res.StatusCode == 200)
            {
                return new EditorResult() { Result = true, Message = $"{msgTemp}修改成功" };
            }
            else
            {
                return new EditorResult() { Result = false, Message = $"{msgTemp}修改失败",Extention=$",响应码:{res.StatusCode}" };
            }
        }
        catch (TeaException error)
        {
            return new EditorResult() { Result = false, Message = $"{msgTemp}修改失败",Extention= error.Message };
        }
        catch (Exception error)
        {
            return new EditorResult() { Result = false, Message = $"{msgTemp}修改失败", Extention = error.Message };
        }
    }
}
